package com.sangmo.fts.process.inventory.service;

import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.inventory.interfaces.IfLadingBillCreation;
import com.sangmo.fts.process.inventory.model.IfContract;
import com.sangmo.fts.process.inventory.model.IfLadingBill;
import com.sangmo.fts.process.inventory.model.IfLadingBillItem;
import com.sangmo.fts.process.inventory.model.IfLoan;
import com.sangmo.fts.process.inventory.model.IfStock;
import com.sangmo.fts.process.inventory.model.IfStockItem;
import com.sangmo.fts.process.inventory.repo.IfLadingBillRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Repository
public class IfLadingBillEntityService extends AuthorityEntityService<IfLadingBill> {

    @Autowired
    private IfLadingBillRepo ifLadingBillRepo;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private IfLoanService ifLoanService;

    @Autowired
    private IfContractService ifContractService;

    @Autowired
    private IfStockService ifStockService;

    @Autowired
    private IfLadingBillItemEntityService ifLadingBillItemEntityService;

    @Override
    protected IDataViewRepo<IfLadingBill> getRepo() {
        return ifLadingBillRepo;
    }

    @Override
    protected void onCreate(IfLadingBill entity, Object bo) {
        super.onCreate(entity, bo);
        IfLadingBillCreation creation = (IfLadingBillCreation) bo;
        IfLoan loan = ifLoanService.get(creation.getLoanId());
        IfContract contract = ifContractService.get(loan.getContractId());
        entity.setContractId(loan.getContractId());
        entity.setPledgee(loan.getBankId());
        entity.setPledgor(contract.getPledgor());
        entity.setRegulator(contract.getRegulator());
    }

    @Override
    protected void afterCreated(IfLadingBill entity, Object bo) {
        super.afterCreated(entity, bo);
        IfLadingBillCreation creation = (IfLadingBillCreation) bo;
        if (null != creation.getBillApplyDoc()) {
            fileUploadService.updateRef(creation.getBillApplyDoc(), ""+entity.getId(), "IfLadingBill.Apply");
        }
        IfLoan loan = ifLoanService.get(creation.getLoanId());
        List<IfStockItem> stockItems = ifStockService.listItemsByStockId(loan.getStockId());
        List<IfLadingBillItem> ladingBillItems = buildIfLadingBillItems(entity.getId(), stockItems);
        ifLadingBillItemEntityService.saveAll(ladingBillItems);
    }

    private List<IfLadingBillItem> buildIfLadingBillItems(Long ladingBillId, List<IfStockItem> stockItems) {
        List<IfLadingBillItem> ladingBillItems = new ArrayList<>();
        for (IfStockItem stockItem : stockItems) {
            IfLadingBillItem item = IfLadingBillItem.fromStockItem(stockItem);
            item.setBillId(ladingBillId);
            ladingBillItems.add(item);
        }
        return ladingBillItems;
    }
}
